<?php

namespace App\Http\Controllers\System;

use App\Http\Controllers\Controller;
use App\Common\MobileMassege;
use App\Models\V1\BookModel;
use App\Repository\Comment;
use Carbon\Carbon;
use DB;
use App\Models\V1\AudioModel;
use App\Common\Jpush;
use App\Models\V1\MessageModel;
use Illuminate\Http\Request;


/***********************************
 * 本文件主要用于系统的一些定时任务
 * 本文件用于一次性编写成功 未完成的任务不要保留在本文档中
 ***********************************/
class CrondController extends Controller
{
    use Comment;

    /**
     * 控制器入口
     * */
    public function enter(Request $request)
    {
        $method = $request->input('method');
        if(empty($method))
        {
            return "方法不能为空";
        }

        if(!method_exists($this,$method))
        {
            return "方法不存在";
        }
        $this->$method($request);
    }



    /**
     * 每日音频极光推送
     */
    public function send_day_video_jpush()
    {
        return "what are you doing?";
        
        $audioModel = new AudioModel();
        $audioInfo  = $audioModel->get_current_day_video();
        if ($audioInfo) {
            //极光推送
            $msg = $audioInfo->AudioTitle;
            //$this->JpushContent($msg, null, '老侯论道每日一言:', ['code' => 205, 'message' => null]);
            $jpush        = new Jpush();
            $jpush->pushContent("老侯论道",$msg,205,0,"",[],[]);
            return 'ok';
        }
    }


    /**
     * 每周三的书籍推送
     * */
    public function wednesday_book_massege()
    {
        $date      = date('Y-m-d');
        //$date      = '2017-7-18';
        $bookModel = new BookModel();
        $bookInfo  = $bookModel->get_date_book($date);
        if ($bookInfo) {

            //极光推送
            $msg = "本周阅读《{$bookInfo->BookTitle}》";
            $jpush = new Jpush();
            $jpush->pushContent('橙色星期三，众智共享日',$msg,206,0,"",[],['bookId'=>$bookInfo->BookID]);


            //1.极光推送206---code
            //$bookId  = $bookInfo->BookID;
            //$message = ['bookId' => $bookId];
            //$data    = ['code' => 206, 'message' => json_encode($message)];
            //$this->JpushContent($msg, null, '橙色星期三，众智共享日', $data);
            return 'ok';
        }
    }


    /**
     * 移动手机号到历史记录中
     * */
    public function move_mobile_code_to_history()
    {
        //找到一条历史记录
        $time   = time()-24*60*60;
        $date   = date('Y-m-d 23:59:59',$time);
        $sql    = "INSERT INTO mobilecode_history SELECT * FROM mobilecode WHERE AddDate <= '$date'";
        DB::insert($sql);
        $sql    = "DELETE FROM mobilecode WHERE AddDate <= '$date'";
        DB::delete($sql);
        return "ok";
    }


    /**
     * 将积分移到历史数据中  每月11号执行
     * 由于积分是按月和按周来显示的 所以在每月的11号将上月的数据移动到历史数据中
     * 并把之前的积分放在10号这一天
     * */
    public function move_member_integral_to_history()
    {

        //截止日期
        $endDate    = date('Y-m-01 00:00:00',strtotime('-1 month'));
        $endDate    = date('Y-m-d 23:59:59',strtotime($endDate)-1);
        $countId    = 1000000;
        $table      = "member_integral";
        $now        = date_time();
        //从头开始统计每个用户的数据
        while(true)
        {
            //1.查找用户 标记 content_id = 100000 代表的是统计得到的积分
            $userInfo = DB::table($table)
                ->where('created_at','<',$endDate)
                ->where('content_id','<>',$countId)
                ->orderBy('id')
                ->first();
            if(!$userInfo)
            {
                break;
            }
            $userId = $userInfo->user_id;
            //2.统计这个人的积分
            $totalIntegral = DB::table($table)
                ->where('user_id',$userId)
                ->where('created_at',"<",$endDate)
                ->sum('integral');


            //3.将总的积分加入到数据表中
            $data   = [
                'user_id'   => $userId,
                'integral'  => $totalIntegral,
                'content_id'=> $countId,
                'note'      => "月统计",
                'created_at'=> $endDate,
                'updated_at'=> $now
            ];

            DB::table($table)->insert($data);

            //4.把数据移到历史记录中
            $sql = "INSERT INTO member_integral_history
                    (SELECT * FROM member_integral
                    WHERE user_id = $userId
                    AND content_id <> $countId
                    AND created_at < '$endDate')";
            DB::insert($sql);

            //删除这个人的数据
            DB::table($table)
                ->where('user_id',$userId)
                ->where('created_at',"<",$endDate)
                ->delete();
        }
    }


    /**
     * 把用户的在线日志移动到历史数据中
     * */
    public function move_member_online_log_to_history()
    {

        //把昨天以前的数据统计起来
        $date   = date_time();

        //将用户ID为0的数据移动到历史中
        $sql = "INSERT INTO member_online_log_history
                SELECT * FROM member_online_log WHERE user_id = 0";
        DB::insert($sql);
        DB::table('member_online_log')->where('user_id',0)->delete();

        while(true)
        {
            //1.找到指定的用户
            $userInfo   = DB::table('member_online_log')
                ->where('begin_time',"<",$date)
                ->where('type',"")
                ->orderBy('id')
                ->first();


            if(!$userInfo)
            {
                break;
            }

            $userId = $userInfo->user_id;

            //2.统计相应的时长
            $totalTime  = DB::table('member_online_log')
                ->where('begin_time',"<",$date)
                ->where('user_id',$userId)
                ->sum('time_length');


            //3.将数据移到历史数据中
            $sql = "INSERT INTO member_online_log_history
                    SELECT * FROM member_online_log
                    WHERE user_id = $userId
                    AND begin_time < '{$date}'
                    AND `type` = '' ";
            DB::insert($sql);

            //4.删除历史数据
            DB::table('member_online_log')
                ->where('begin_time',"<",$date)
                ->where('user_id',$userId)
                ->delete();

            //将新的数据加入到记录中
            $data   = [
                'user_id'       => $userId,
                'begin_time'    => $date,
                'end_time'      => $date,
                'time_length'   => $totalTime,
                'type'          => 'total'
            ];
            DB::table('member_online_log')
                ->insert($data);
        }

    }



}
